package wbbs.web.admin;

import cn.webwheel.Action;
import cn.webwheel.results.ErrorResult;
import cn.webwheel.results.TemplateResult;
import com.google.inject.Inject;
import wbbs.domain.BoardCategory;
import wbbs.service.BoardService;

import java.sql.SQLException;

public class category extends BaseAction {

    public BoardCategory category;

    @Inject
    BoardService boardService;

    @Action
    public Object html(int id) throws SQLException {

        ensureLoginPage();

        if (id == 0) {
            category = new BoardCategory();
        } else {
            category = boardService.findCategory(id);
            if (category == null) {
                return new ErrorResult(404);
            }
        }
        return new TemplateResult(this);
    }

    @Action
    public Object update() throws SQLException {

        ensureLoginAction();

        range(notNull(nospace(trim(category.name))), 1, 32);
        range(notNull(trim(category.remark)), 1, 512);

        if (category.id == 0) {
            boardService.insertCategory(category);
        } else {
            boardService.updateCategory(category);
        }
        return ok().set("id", category.id);
    }

}
